package com.example.export.service.controller;


import com.example.export.service.JxdNewYnService;
import com.example.export.util.ResultAo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping(value = "export")
public class GdsjsdController extends BaseController {

    @Resource
    private JxdNewYnService jxdNewYnService;

    @PostMapping(value = "/excel.do")
    public void exportJxdNew(HttpServletResponse response) {
        HSSFWorkbook workbook = null;
        try {
            ResultAo resultAo = jxdNewYnService.getJxdWorkbookNewYn();
            if (resultAo.getSuccess()) {
                // 导出的文件名
                String fileName = "票证结报缴销信息 " + (new SimpleDateFormat("yyyyMMdd").format(new Date())) + ".xls";
                // 导出
                workbook = (HSSFWorkbook) resultAo.getData();
                this.downloadExcel(response, workbook, fileName);
            }
        } catch (Exception e) {
            log.error("[国地税税款]导出税收票款结报缴销单失败", e);
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
                //ignore
            }
        }
    }


}
